package cn.huobi.org.controller;

import cn.huobi.basic.util.AjaxResult;
import cn.huobi.basic.util.LoginContext;
import cn.huobi.basic.util.PageList;
import cn.huobi.org.domain.Employee;
import cn.huobi.org.query.EmployeeQuery;
import cn.huobi.org.service.EmployeeService;
import cn.huobi.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 查询所有
     */
    @GetMapping
    @ApiOperation(value = "查询所有员工数据" )
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /**
     * 查询单条
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee findById(@PathVariable("id") Long id){
        return employeeService.findById(id);
    }

    /**
     * 添加或修改
     */
    @PutMapping
    public AjaxResult add(@RequestBody Employee employee){
        if (employee.getId() == null){
            // 添加
            employeeService.add(employee);
        }else {
            // 修改
            employeeService.update(employee);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult detele(@PathVariable("id") Long id){
        employeeService.delete(id );
        return AjaxResult.me().success();
    }

    /**
     * 分页查询
     */
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return employeeService.queryByPage(query);
    }

    /**
     *  批量删除
     */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        employeeService.batchDel(ids);
        return AjaxResult.me().success();
    }

    /**
     * 查询部门员工
     */
    @GetMapping("/departmentId/{departmentId}")
    public List<Employee> findByDepartmentId(@PathVariable("departmentId") Long departmentId){
        return employeeService.findByDepartmentId(departmentId);
    }

    @GetMapping("/findByShopId")
    public List<Employee> findByShopId(HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLoginContext(request);
        Employee employee = employeeService.findByLogininfoId(logininfo.getId());
        return employeeService.findByShopId(employee.getShopId());
    }
}
